package com.nf.health.app.bodyfat;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.bodyfat.bean.BodyFatInfo;
import com.nf.health.app.bodyfat.bean.IndexNormalBean;
import com.nf.health.app.bodyfat.util.BodyFatUtil;
import com.nf.health.app.models.UserInfo;
import com.nf.health.app.utils.DateUtil;
import com.nf.health.app.utils.PreferenceHelper;
import com.nf.health.app.utils.ViewUtil;

import java.util.Calendar;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hepann on 2016/3/28.
 * The only genius that is worth anything is the genius for hard work
 *
 * @author hepann
 * @Description 体脂数据测量结果类
 * @emial he_pann@163.com
 */
public class BodyFatMeasuredResultActivity extends BaseActivity {
    @Bind(R.id.iv_measured_result_bk)
    ImageView ivMeasuredResultBk;
    @Bind(R.id.tv_date)
    TextView tvDate;
    @Bind(R.id.tv_weight)
    TextView tvWeight;
    @Bind(R.id.tv_fat_value)
    TextView tvFatValue;
    @Bind(R.id.tv_water_value)
    TextView tvWaterValue;
    @Bind(R.id.tv_heat_value)
    TextView tvHeatValue;
    @Bind(R.id.tv_skeleton_value)
    TextView tvSkeletonValue;
    @Bind(R.id.tv_muscle_value)
    TextView tvMuscleValue;
    @Bind(R.id.tv_viscus_fat_value)
    TextView tvViscusFatValue;
    @Bind(R.id.tv_body_age)
    TextView tvBodyAge;
    @Bind(R.id.btn_measure)
    Button btnMeasure;
    @Bind(R.id.tv_skeleton_normal)
    TextView tvSkeletonNormal;
    @Bind(R.id.tv_water_normal)
    TextView tvWaterNormal;
    @Bind(R.id.tv_heat_normal)
    TextView tvHeatNormal;
    @Bind(R.id.tv_muscle_normal)
    TextView tvMuscleNormal;
    @Bind(R.id.tv_viscus_fat_normal)
    TextView tvViscusFatNormal;
    @Bind(R.id.tv_fat_normal)
    TextView tvFatNormal;

    private int images[] = {
            R.drawable.tzc1,
            R.drawable.tzc2,
            R.drawable.tzc3,
            R.drawable.tzc4,
            R.drawable.tzc5,
            R.drawable.tzc6,
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_body_fat_measured_result);
        ButterKnife.bind(this);

        ViewUtil.setText(this, R.id.titlebar_title_tv, "测量结果");
        btnMeasure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        BodyFatInfo info = PreferenceHelper.getBodyFatInfo();
        //显示测量结果
        initData(info);
        //显示标准范围
        initNormalValue(info);
        //根据脂肪率判定健康等级
        int level = levelJudgmentByFat(Float.valueOf(info.getWeight()));
        ivMeasuredResultBk.setBackgroundResource(images[level]);
        tvDate.setText(DateUtil.date2yyyyMMdd(new Date()));
    }

    private void initNormalValue(BodyFatInfo info) {
        UserInfo userInfo = PreferenceHelper.getUser();
        String height = userInfo.getHeight();
        String sex = userInfo.getSex();
        int age = 0;
        if (!TextUtils.isEmpty(userInfo.getBirthday())) {
            try {
                int year = Integer.parseInt(userInfo.getBirthday().split("-")[0]);
                age = Calendar.getInstance().get(Calendar.YEAR) - year;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        IndexNormalBean normalBean = BodyFatUtil.getIndexNormarValue(Float.valueOf(info.getWeight()), height, sex, age + "");
        tvSkeletonNormal.setText(normalBean.getSkeleton());
        tvMuscleNormal.setText(normalBean.getMuscle());
        tvWaterNormal.setText(normalBean.getWater());
        tvViscusFatNormal.setText(normalBean.getViscusFat());
        tvHeatNormal.setText(normalBean.getHeat());
        tvFatNormal.setText(normalBean.getFat());
    }

    /**
     * @param weight
     * @return 健康等级
     */
    private int levelJudgmentByFat(float weight) {
        float height = Float.valueOf(PreferenceHelper.getString(PreferenceHelper.USERHEIGHT, "175"));
        float fact = weight * 10000 / (height * height);
        int level = 1;//默认正常
        if (fact == 0) {
            level = 1;
        } else if (fact <= 18.5) {
            level = 0;
        } else if (fact <= 23.9 && fact > 18.5) {
            level = 1;
        } else if (fact <= 26.9 && fact > 23.9) {
            level = 2;
        } else if (fact <= 30 && fact > 26.9) {
            level = 3;
        } else if (fact <= 39.9 && fact > 30) {
            level = 4;
        } else if (fact > 39.9) {
            level = 5;
        }
        return level;
    }


    /**
     * 显示测量结果
     * 对象在sp中
     * 对应的参数类型有些混乱
     */
    private void initData(BodyFatInfo info) {
        tvWeight.setText(info.getWeight());
        tvBodyAge.setText(info.getAge());
        tvWaterValue.setText(info.getShuiFen());
        tvViscusFatValue.setText(info.getPiXiaZhiFang());
        tvFatValue.setText(info.getFat());
        tvMuscleValue.setText(info.getJiRou());
        tvSkeletonValue.setText(info.getBone());
        tvHeatValue.setText(info.getbMR());
    }
}
